# -*- coding: iso-8859-1 -*-
#
#    This is part of Malvinas Argentinas Messenger
#
#    Malvinas Argentinas Messenger is free software; you can redistribute
#    it and/or modify it under the terms of the GNU General Public License
#    as published by the Free Software Foundation; either version 2 of the
#    License, or (at your option) any later version.
#
#    Malvinas Argentinas Messenger is distributed in the hope that it will be
#    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Malvinas Argentinas Messenger; if not, write to the Free
#    Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
#    MA  02110-1301  USA

import tab
import pad

# List of tabs
class Array (object):
	def __init__(self, rows, cols, y, x):
		self.y = y
		self.x = x
		self.rows = rows
		self.cols = cols
		self.wins = [] # Array of tab objects
		self.idactive = 0
		
	def resize(self, sizeY, sizeX, y, x):
		self.y = y
		self.x = x
		self.rows = sizeY
		self.cols = sizeX
		for item in self.wins:
			item.ventana.resize(sizeY, sizeX, y, x)
			
	def dump(self, chat, file):
		self.wins[chat].dump(file)
		
	def newWin(self, name, passport = ''):
		Item = tab.Tab()
		Item.set_ventana(pad.Pad(self.rows, self.cols, self.y, self.x))
		Item.set_nombre(name)
		Item.set_passport(passport)		
		self.wins.append(Item)
		
	def hasPass(self, name):
		count = 0
		found = 0
		for i in self.wins:
			count += 1
			if i.passport == name:
				found = 1
				break
		if found:
			return count
		else:
			return -1
			
	def delTab(self, numPest):
		self.wins.pop(numPest)
		
	def addstr(self, name, text, attr = None):
		num = self.NomtoId(name)
		w = self.wins[num]
		w.ventana.addstr(text, attr)
		
	def scrollUp (self, chat, lines = 1):
		w = self.wins[chat]
		w.scrollUp(lines)
		
	def scrollDown (self, chat, lines = 1):
		w = self.wins[chat]
		w.scrollDown(lines)
		
	def toTop(self, chat):
		self.wins[chat].toTop()
	
	def toBottom(self, chat):
		self.wins[chat].toBottom()
		
	def redraw(self):
		for w in self.wins:
			self.refresh(w.nombre)
			
	def refresh(self, name):
		w = self.wins[self.NomtoId (name)].ventana
		w.refresh()
		
	def NomtoId(self, name):
		result = ''
		for item in self.wins:
			if item.nombre == name:
				result = self.wins.index (item)
		if result == '':
			return -1
		else:
			return result

	def IdtoNom(self, id):
		if id > len(self.wins):
			return -1
		else:
			return self.wins[id].nombre

	def size(self):
		return len(self.wins)
